
package GUIs;

import javax.persistence.EntityManager;
import javax.swing.JOptionPane;

public class LoginGUI extends javax.swing.JFrame {
    
    private EntityManager em;
       
    public LoginGUI(EntityManager em) {
        this.em = em;
        initComponents();
        setLocationRelativeTo(null); // Κεντράρει το παράθυρο στην οθόνη
    }

   
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        Panel_Login = new javax.swing.JPanel();
        Label_Login_Welcome = new javax.swing.JLabel();
        Label_Username = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        TextField_Username = new javax.swing.JTextField();
        PasswordField = new javax.swing.JPasswordField();
        Button_LoginCommand = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setBackground(new java.awt.Color(255, 102, 204));

        Panel_Login.setBackground(new java.awt.Color(153, 0, 255));

        Label_Login_Welcome.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        Label_Login_Welcome.setForeground(new java.awt.Color(255, 204, 204));
        Label_Login_Welcome.setText("LOGIN");

        javax.swing.GroupLayout Panel_LoginLayout = new javax.swing.GroupLayout(Panel_Login);
        Panel_Login.setLayout(Panel_LoginLayout);
        Panel_LoginLayout.setHorizontalGroup(
            Panel_LoginLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, Panel_LoginLayout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(Label_Login_Welcome)
                .addGap(165, 165, 165))
        );
        Panel_LoginLayout.setVerticalGroup(
            Panel_LoginLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, Panel_LoginLayout.createSequentialGroup()
                .addContainerGap(23, Short.MAX_VALUE)
                .addComponent(Label_Login_Welcome)
                .addGap(21, 21, 21))
        );

        Label_Username.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        Label_Username.setText("ΟΝΟΜΑ ΧΡΗΣΤΗ");
        Label_Username.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED, null, new java.awt.Color(255, 204, 204), null, null));

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel3.setText("ΚΩΔΙΚΟΣ ΧΡΗΣΤΗ");
        jLabel3.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED, null, new java.awt.Color(255, 204, 204), null, null));

        TextField_Username.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                TextField_UsernameActionPerformed(evt);
            }
        });

        PasswordField.setText("jPasswordField1");
        PasswordField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                PasswordFieldActionPerformed(evt);
            }
        });

        Button_LoginCommand.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        Button_LoginCommand.setText("ΕΙΣΟΔΟΣ");
        Button_LoginCommand.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Button_LoginCommandActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(Panel_Login, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addGap(237, 237, 237))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(Label_Username, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(TextField_Username)
                            .addComponent(PasswordField, javax.swing.GroupLayout.DEFAULT_SIZE, 198, Short.MAX_VALUE))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(0, 0, Short.MAX_VALUE)
                .addComponent(Button_LoginCommand)
                .addGap(137, 137, 137))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(Panel_Login, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(48, 48, 48)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(Label_Username, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(TextField_Username, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(41, 41, 41)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, 29, Short.MAX_VALUE)
                    .addComponent(PasswordField))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 32, Short.MAX_VALUE)
                .addComponent(Button_LoginCommand))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void PasswordFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_PasswordFieldActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_PasswordFieldActionPerformed

    private void TextField_UsernameActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_TextField_UsernameActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_TextField_UsernameActionPerformed

    private void Button_LoginCommandActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_Button_LoginCommandActionPerformed
        
        // Για να δώσει αποτέλεσμα το κουμπί ΕΙΣΟΔΟΣ (Button_LoginCommand) πρέπει
        // να ανακτηθούν το όνομα και ο κωδικός του χρήστη και να ελεγχθούν
        String username = TextField_Username.getText();
        String password = new String(PasswordField.getPassword());
        
        // Έλεγχος εισόδου ονόματος και κωδικού χρήστη
        if(username.equals("")){
            JOptionPane.showMessageDialog(this, "Πρέπει να εισάγετε το όνομα χρήστη","Εισαγωγή Δεδομένων",JOptionPane.WARNING_MESSAGE);
            return;
        }
        
        if(password.equals("")){
            JOptionPane.showMessageDialog(this, "Πρέπει να εισάγετε τον κωδικό χρήστη","Εισαγωγή Δεδομένων",JOptionPane.WARNING_MESSAGE);
            return;
        }
        
        // Έλεγχος για διαχειριστή. Θέτω ως όνομα και κωδικό του Διαχειριστή το aggm
        if(username.equals("aggm") && password.equals("aggm")){
        	// Ανοίγει η φόρμα του διαχειριστή
        	AdministratorGUI ag = new AdministratorGUI(em);
        	ag.setVisible(true);
        	// Σβήνει το τρέχον παράθυρο λογκίν
        	dispose();
        	//Βγαίνω από τη μέθοδο Button_Login με return
        	return;
        }

        // Έλεγχος για χρήστη. Όνομα χρήστη είναι ο κωδικός κάρτας πόντων 
        // και κωδικός αυτός που έχει εισαχθεί στη ΒΔ
        //
    }//GEN-LAST:event_Button_LoginCommandActionPerformed

 
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton Button_LoginCommand;
    private javax.swing.JLabel Label_Login_Welcome;
    private javax.swing.JLabel Label_Username;
    private javax.swing.JPanel Panel_Login;
    private javax.swing.JPasswordField PasswordField;
    private javax.swing.JTextField TextField_Username;
    private javax.swing.JLabel jLabel3;
    // End of variables declaration//GEN-END:variables
}
